import {Component, OnInit} from '@angular/core';
import {PreviewsService} from './previews.service';
import {HttpService} from '@kylin/http';
import {CacheService} from '@kylin/cache';
import {NzMessageService} from 'ng-zorro-antd';

@Component({
    templateUrl: './previews.component.html',
    styleUrls: ['./previews.component.less']
})
export class PreviewsComponent implements OnInit {
    sidebarData: Array<any>;

    constructor(private previewsService: PreviewsService,
                private http: HttpService,
                private cacheService: CacheService,
                private nzMessageService: NzMessageService) {
    }

    ngOnInit() {
        this.sidebarData = this.previewsService.getSidebarData();
        this.fetchUserInfo();
    }

    logout() {
        this.http.delete('/remove_token', {access_token: this.cacheService.get('access_token')})
            .then(res => {

            })
            .catch(err => {

            })
    }

    fetchUserInfo() {
        this.http.get('http://192.168.1.188:8000/users/current', {access_token: this.cacheService.get('access_token')})
            .then(res => {
                console.log(`获取用户信息成功`);
                console.log(res);
            })
            .catch(err => {

            })
    }

    fetchAccessToken() {
        const body = {
            'refresh_token': this.cacheService.get('refresh_token'),
            'clientId': 'system',
            'clientSecret': 'system',
            'scope': 'app',
            'grantType': 'refresh_token'
        };
        this.http.post('/oauth/user/token', body)
            .then(res => {
                if (res.resp_code === '10000') {
                    this.nzMessageService.success('获取新access_token成功');
                } else {
                    this.nzMessageService.error(res.resp_msg);
                }
            })
            .catch(err => {

            })
    }

}
